<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : statics.class.php   FILE_PATH : \\classes\application\statics.class.php
 * ....访问统计
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Dec 18 16:48:47 CST 2007
 */
class StaticsClass  extends FrameWork {
	var $data_path;

	function StaticsClassStruct($url_array){
		/**
		 * 统计文件目录
		 */
		$document_root = @preg_replace("/http:\/\/{$_SERVER['HTTP_HOST']}/", "", $url_array['site_url']);
		$this->data_path = '..'.$url_array['datapath'];
		$this->ip_path = $_SERVER['DOCUMENT_ROOT'].$document_root.$url_array['ippath'];
	}
	/**
	 * 得到统计起始日期
	 *
	 * @return string $start_date
	 */
	function getStartDate(){
		$start_date_file = $this->data_path . "/day1.dat";
		$start_date = @file($start_date_file);
		return $start_date[0];
	}
	/**
	 * 取得当日IP统计
	 * $time 格式为年/月/日
	 * @return array
	 */
	function getTodayIp($time=''){
		if ($time != '') {
			$fileurl = $this->data_path .'/'.$time. "/ip_all.dat";
		}else {
			$fileurl = $this->data_path .'/'.date('Y').'/'.date('n').'/'.date('j'). "/ip_all.dat";
		}
		$garray = @file($fileurl);
		if (is_array($garray)) {
			foreach ($garray as $k => $v){
				$v = explode('|',$v);
				$ip[$k][ip] = $v[0];
				$ip[$k][num] = $v[1];
				$num += $v[1];
			}
		}
		/*倒序*/
		for ($i=0;$i<count($ip);$i++) 
		{
		    for ($j=count($ip)-2;$j>=$i;$j--) 
		    { 
		    	if($ip[$j+1][num]>$ip[$j][num])
		    	{
		    		$tmp = $ip[$j+1];
		    		$ip[$j+1]=$ip[$j];
		    		$ip[$j]=$tmp;
		    	}
		    }
		}
		$array['ip'] = $ip;
		$array['num'] = $num;
		return $array;
	}

	/**
	 * 获得当日访问人数
	 *
	 * @return int $ipnum
	 */
	function getIpNum(){
		$fileurl =  $this->data_path.'/'.date('Y').'/'.date('n').'/'.date('j'). "/ip.dat";
		$garray = @file($fileurl);
		$larray = explode("|", $garray[0]);
		for($i=0; $i<24; $i++){
			$ipnum = $ipnum + $larray[$i];
		}
		return $ipnum;
	}

	/**
	 * 获得当日页面浏览量
	 *
	 * @return array $pvnum
	 */
	function getPvNum(){
		$fileurl = $this->data_path .'/'.date('Y').'/'.date('n').'/'.date('j'). "/pv.dat";
		$garray = @file($fileurl);
		$larray = explode("|", $garray[0]);
		for($i=0; $i<24; $i++){
			$pvnum+= $larray[$i];
		}
		return $pvnum;
	}

	/**
	 * 取得统计天数
	 *
	 * @return int
	 */
	function getDayNum(){
		$fileurl = $this->data_path . "/day2.dat";
		$fp = @fopen($fileurl, "r");
		$daynum = fgets($fp, 6);
		fclose($fp);
		return $daynum;
	}
	/**
	 * 取得最高日访问量
	 *
	 * @return int
	 */
	function getMaxIpNum(){
		$fileurl = $this->data_path . "/pvip_top.dat";
		if (file_exists($fileurl)) {
			$garray = @file($fileurl);
			$garray = explode("|", $garray[1]);
			if ($garray[1] != 0){
				$garray[1] = date('Y-m-d',$garray[1]);
			}
		}
		return $garray;
	}
	/**
	 * 取得最高日页面浏览量
	 *
	 * @return int
	 */
	function getMaxPvNum(){
		$fileurl = $this->data_path . "/pvip_top.dat";
		if (file_exists($fileurl)) {
			$garray = @file($fileurl);
			$garray = explode("|", $garray[0]);
			if ($garray[1] != 0){
				$garray[1] = date('Y-m-d',$garray[1]);
			}
		}
		return $garray;
	}
	/**
	 * 取得访问总人数和浏览量总数
	 *
	 * @return int
	 */
	function getNumSum(){

		$dh = @opendir($this->data_path);
		While(false !==($flist=@readdir($dh))){
			if (is_dir($this->data_path.'/'.$flist) && is_numeric($flist)){/*取年份文件夹*/
				$dir = $this->data_path.'/'.$flist.'/pvip_ye.dat';
				if (file_exists($dir)) {
					$farray = @file($dir);
					/*PV*/
					$array1 = explode('|',$farray[2]);
					foreach ($array1 as $v){
						$num['pv'] += $v;
					}
					/*IP*/
					$array2 = explode('|',$farray[3]);
					foreach ($array2 as $v){
						$num['ip'] += $v;
					}
				}
			}
		}
		return $num;
	}
	/**
	 * 获得总页面浏览时段数组
	 *
	 * @return array
	 */
	function getIpPvSumNumArray(){
		$fileurl = $this->data_path . "/pvip.dat";
		$garray = @file($fileurl);
		$ipall = explode("|",$garray[1]);
		$pvall = explode("|",$garray[0]);
		for($i=0; $i<24; $i++){
			$ippvall[$i][hour] = $i;
			$ippvall[$i][ipnum] = $ipall[$i];
			$ippvall[$i][pvnum] = $pvall[$i];
		}
		return $ippvall;
	}
	/**
	 * 取得某时段浏览
	 *
	 * @return unknown
	 */
	function getSomeTimeNum($time){
		$fileurl = $this->data_path .'/'.$time . "/pvip.dat";
		if (file_exists($fileurl)) {
			$garray = @file($fileurl);
			$array_pv = explode("|", $garray[0]);
			$array_ip= explode("|", $garray[1]);
			foreach ($array_pv as $k => $v){
				if ($k != '24') {
					$array[$k]['pv'] = $array_pv[$k];
					$array[$k]['ip'] = $array_ip[$k];
				}
			}
		}else {
			for ($i=0;$i<24;$i++){
				$array[$i]['pv'] = 0;
				$array[$i]['ip'] = 0;
			}
		}
		return $array;
	}
	/**
	 * IP地区识别
	 *
	 * @param string $ip
	 * @return string
	 */
	function getIp($ip){
		$lang_obj = new FrameWork();
		$lang_obj->setFrameWork();
		$lang_obj->getlang("sys_common");
		$flag = 0;
		$ip_detail = explode(".",$ip);
		if (is_array($ip_detail)){
			foreach ($ip_detail as $key => $value){
				$ip_new[$key] = str_pad($value, 3, "0", STR_PAD_LEFT);
			}
		}
		if (file_exists($this->ip_path . "/" . $ip_detail[0] . ".txt")) {
			$ip_fdata = fopen($this->ip_path . "/" . $ip_detail[0] . ".txt", "r");
		}
		else {
			if(!($ip_fdata = fopen($this->ip_path . "/0.txt","r"))) {
				die($lang_obj->_lang['langSysCIPDataFileErr']);
			}
		}

		$ip = join(".", $ip_new);
		while(!feof($ip_fdata)){
			$ip_data = fgets($ip_fdata,200);
			$ip_data_detail = explode("|",$ip_data);
			if(($ip >= $ip_data_detail[0]) and ($ip <= $ip_data_detail[1])) {
				fclose($ip_fdata);
				$ip_detail = $ip_data_detail[2];
				$flag = 1;
				break;
			}
		}
		if($flag == 0){
			$ip_detail = $lang_obj->_lang['langSysCSealedZone'];
		}
		unset($lang_obj);
		return $ip_detail;
	}
	
	/**
	 * 取得最近访客记录
	 *
	 * @return unknown
	 */
	function getVisitArray($time){
		$fileurl = $this->data_path .'/' . $time . "/from.dat";
		if (file_exists($fileurl)) {
			$garray = @file($fileurl);
			$cog = count($garray);
			for($i=0; $i<$cog; $i++) {
				$larray[$i] = explode("|",$garray[$i]);
				$larray[$i][4] = $this->getIp($larray[$i][1]);
			}
		}
		return $larray;
	}
	/**
	 * 取得详细IP
	 *
	 * @return array
	 */
	function getIpDetail(){
		$fileurl = $this->data_path . "/pvip.dat";
		$garray = @file($fileurl);
		$ipall = explode("|",$garray[1]);
		return $ipall;
	}
	/**
	 * 取得详细页面浏览
	 *
	 * @return array
	 */
	function getPvDetail(){
		$fileurl = $this->data_path . "/pvip.dat";
		$garray = @file($fileurl);
		$pvall = explode("|",$garray[0]);
		return $pvall;
	}

	/**
	 * 来源统计
	 *
	 * @param 
	 * @return array
	 */
	function getFromArray($start_time,$end_time){
		$i = 0;
		$sign = 0;
		for ($start_time ; $start_time > $end_time ; $start_time=$start_time-24*60*60){
			$daturl = $this->data_path.'/'.date('Y/n/j',$start_time).'/file_fm.dat';
			if (file_exists($daturl)) {
				$garray = @file($daturl);
				if (is_array($garray)) {
					foreach ($garray as $v){
						$line = explode('|',$v);
						/*判断已有数组中是否存在该链接，如果存在，则在已有位置中加数量*/
						if (is_array($array)) {
							foreach ($array as $k => $v2){
								if ($v2['url'] == $line[2]) {
									$array[$k]['num'] += intval($line[0]);
									$sign = 1;
								}
							}
						}
						if ($sign != 1) {/*则说明当前数组中没有该来源页面,在数组中新增*/
							$array[$i]['num'] = intval($line[0]);
							$array[$i]['url'] = $line[2];
							$i++;
						}else {
							$sign = 0;
						}
					}
				}
			}
		}
		/*倒序排列*/
		for ($i=0;$i<count($array);$i++) 
		{
		    for ($j=count($array)-2;$j>=$i;$j--) 
		    { 
		    	if($array[$j+1][num]>$array[$j][num])
		    	{
		    		$tmp = $array[$j+1];
		    		$array[$j+1]=$array[$j];
		    		$array[$j]=$tmp;
		    	}
		    }
		}
		return $array;
	}
	
	/**
	 * 来源页面趋势对比
	 * @author 
	 * @param  $start_time开始时间,$end_time结束时间,$url对比趋势链接
	 * @return array
	 */
	function getFromTrend($start_time,$end_time,$url){
		if ($url != '') {
			for ($start_time ; $start_time > $end_time ; $start_time=$start_time-24*60*60){
				$daturl = $this->data_path.'/'.date('Y/n/j',$start_time).'/file_fm.dat';;
				if (file_exists($daturl)) {
					$garray = @file($daturl);
					if (is_array($garray)){
						foreach ($garray as $k => $v){
							$line = explode('|',$v);
							foreach ($url as $k2 => $v2){
								$start_time_date = @date("Y-m-d",$start_time);
								//if (strstr($line[2],$v2)) {/*如果包含*/
								if ($line[2] == $v2) {/*如果包含*/
									$url_array[$start_time_date][$k2]['pv'] += intval($line[0]);
								}else {
									$url_array[$start_time_date][$k2]['pv'] = 0;
								}
								unset($start_time_date);						
							}
						}
					}
				}
			}
		}
		return $url_array;
	}
	
	/**
	 * 被访页面分析
	 * @author 
	 * @param  $date
	 * @return array
	 */
	function getGoArray($date){
		$i = 0;
		$daturl = $this->data_path.'/'.$date.'/file_go.dat';;
		if (file_exists($daturl)) {
			$garray = file($daturl);
			if (is_array($garray)){
				foreach ($garray as $k => $v){
					$line = explode	('|',$v);
					$array[$i]['url'] = $line[1];
					$array[$i]['pv'] = intval($line[0]);
					$i++;
				}
			}
		}
		
		/*倒序排列*/
		for ($i=0;$i<count($array);$i++) 
		{
		    for ($j=count($array)-2;$j>=$i;$j--) 
		    { 
		    	if($array[$j+1][pv]>$array[$j][pv])
		    	{
		    		$tmp = $array[$j+1];
		    		$array[$j+1]=$array[$j];
		    		$array[$j]=$tmp;
		    	}
		    }
		}
		
		return $array;
	}
	
	/**
	 * 被访页面趋势
	 * @author 
	 * @param  
	 * @return array
	 */
	function getTrendGo($start_time,$end_time,$url){
		for ($start_time ; $start_time <= $end_time ; $start_time=$start_time+24*60*60){
			$daturl = $this->data_path.'/'.date('Y/n/j',$start_time).'/file_go.dat';
			if (file_exists($daturl)) {
				$garray = @file($daturl);
				if (is_array($garray)){
					foreach ($garray as $k => $v){
						$line = explode('|',$v);
						if ($url == $line[1]) {
							$array[$start_time]['pv'] = intval($line[0]);
						}
					}
				}
			}
		}
		return $array;
	}

	/**
	 * 指定IP趋势
	 * @author 
	 * @param  
	 * @return array
	 */
	function getTrendIp($ip,$start_time,$end_time){
		for ($start_time ; $start_time <= $end_time ; $start_time=$start_time+24*60*60){
			$date = date('Y/n/j',$start_time);
			if (file_exists($this->data_path.'/'.$date.'/ip_all.dat')) {
				$garray = file($this->data_path.'/'.$date.'/ip_all.dat');
				if (is_array($garray)) {
					foreach ($garray as $v){
						$line = explode('|',$v);
						if ($line[0] == $ip) {
							$array[$start_time]['num'] = $line[1];
							$num += $line[1];
						}
					}
				}
			}
		}//print_r($array);
		$ip = array();
		$ip['ip'] = $array;
		$ip['num'] = $num;
		return $ip;
	}
	
	/**
	 * 关键词
	 * @author 
	 * @param  $start_time 开始时间,$end_time 结束时间
	 * @return array
	 */
	function getKeyArray($start_time,$end_time){
		$i = 0;
		$key_sign = 0;
		for ($start_time ; $start_time > $end_time ; $start_time=$start_time-24*60*60){
			$date = date('Y/n/j',$start_time);
			if (file_exists($this->data_path.'/'.$date.'/file_key.dat')) {
				$garray = file($this->data_path.'/'.$date.'/file_key.dat');
				if (is_array($garray)){
					foreach ($garray as $k => $v){
						$array = explode('|',$v);
						if (is_array($key_array)) {
							foreach ($key_array as $k2 => $v2){
								if ($v2['key'] == $array[1]){
									$key_array[$k2]['num'] += intval($array[0]);
									$key_sign = 1;
								}
							}
							/*如果不包含该关键字*/
							if ($key_sign != 1) {
								if (intval($array[0]) && $array[1] && $array[2]){
									$array_num = count($key_array);
									$key_array[$array_num]['num'] = intval($array[0]);
									$key_array[$array_num]['key'] = $array[1];
									$key_array[$array_num]['url'] = $array[2];
								}
							}else {
								$key_sign = 0;
							}
						}else {/*新增关键字*/
							if (intval($array[0]) && $array[1] && $array[2]){
								$key_array[0]['num'] = intval($array[0]);
								$key_array[0]['key'] = $array[1];
								$key_array[0]['url'] = $array[2];
							}
						}
					}
				}
			}
		}
		
		/*倒序排列*/
		for ($i=0;$i<count($key_array);$i++) 
		{
		    for ($j=count($key_array)-2;$j>=$i;$j--) 
		    { 
		    	if($key_array[$j+1][num]>$key_array[$j][num])
		    	{
		    		$tmp = $key_array[$j+1];
		    		$key_array[$j+1]=$key_array[$j];
		    		$key_array[$j]=$tmp;
		    	}
		    }
		}
		return $key_array;
	}
	
	/**
	 * 关键词趋势
	 * @author 
	 * @param  
	 * @return array
	 */
	function getKeyTrend($start_time,$end_time,$key){
		$i = 0;
		$key_sign = 0;
		for ($start_time ; $start_time < $end_time ; $start_time=$start_time+24*60*60){
			$date = date('Y/n/j',$start_time);
			if (file_exists($this->data_path.'/'.$date.'/file_key.dat')) {
				$garray = file($this->data_path.'/'.$date.'/file_key.dat');
				if (is_array($garray)){
					foreach ($garray as $k => $v){
						$array = explode('|',$v);
						if ($key == $array[1]) {
							$key_array[$start_time]['pv'] =  intval($array[0]);
						}
					}
				}else {
					$key_array[$start_time]['pv'] =  0;
				}
			}else {
				$key_array[$start_time]['pv'] =  0;
			}
		}
		return $key_array;
	}
	
	
	/**
	 * 搜索引擎分析
	 * @author   
	 * @param  $start_time,$end_time
	 * @return array
	 */
	function getEngArray($start_time,$end_time){
		for ($start_time;$start_time>$end_time;$start_time=$start_time-24*60*60){
			$daturl = $this->data_path.'/'.date('Y/n/j',$start_time).'/file_key.dat';
			if (file_exists($daturl)) {
				$garray = file($daturl);
				if (is_array($garray)){
					foreach ($garray as $k => $v){
						$line = explode('|',$v);
						if (strstr($line[2],'baidu')) {/*百度*/
							$array['baidu'] += intval($line[0]);
						}else if (strstr($line[2],'google')) {/*google*/
							$array['google'] += intval($line[0]);
						}else {/*其他*/
							$array['other'] += intval($line[0]);
						}
					}
				}
			}
		}
		return $array;
	}
	
	/**
	 * 搜索引擎趋势
	 * @author 
	 * @param  $start_time 开始时间,$end_time 结束时间,$eng 搜索引擎
	 * @return array
	 */
	function getEngTrend($start_time,$end_time,$eng){
		$i=0;
		for ($start_time;$start_time<$end_time;$start_time=$start_time+24*60*60){
			$daturl = $this->data_path.'/'.date('Y/n/j',$start_time).'/file_key.dat';
			if (file_exists($daturl)) {
				$garray = file($daturl);
				if (is_array($garray)){
					foreach ($garray as $k => $v){
						$array = explode('|',$v);
						if (strstr($array[2],$eng)){
							$num[$start_time] = intval($array[0]);
						}
					}
				}else {
					$num[$start_time] = 0;
				}
			}else {
				$num[$start_time] = 0;
			}
		}
		
		return $num;
	}
	
	
	/**
	 * 客户端分析
	 * @author     
	 * @param  $fileurl 文件位置
	 * @return array
	 */
	function getSystemArray($fileurl){
		if (file_exists($fileurl)) {
			$garray = @file($fileurl);
			if (is_array($garray)) {
				foreach ($garray as $k => $v){
					$line = explode('|',$v);
					$system_array[$k]['pv'] = intval($line[0]);
					$system_array[$k]['name'] = $line[1];
				}
			}
		}
		return $system_array;
	}
	
	/**
	 * 输出XML格式的内容
	 * @author 
	 * @param  $input_param,$Vertical_key竖坐标,$Abscissa_key横坐标
	 * @return string
	 */
	function echo_statics($input_param,$Vertical_key,$Abscissa_key){
		
		$line .= "<data>";
		if (is_array($input_param)){
			foreach ($input_param as $k => $v){
				$line .= "<record>
							<navigate displayname='".$Abscissa_key."'>".$v[$Abscissa_key]."</navigate>
							<value displayname='".$Vertical_key."'>".$v[$Vertical_key]."</value>
						</record>";
			}
		}
		$line .= "</data>";
		
		//写入文件
		$fp = @fopen("../share/statics.date.php",'w+');
		flock($fp,LOCK_EX);
		fwrite($fp,$line);
		flock($fp,LOCK_UN);
		fclose($fp);
		return true;
	}
	
	/**
	 * 删除统计文件夹
	 * @author    
	 * @param  
	 * @return bool
	 */
	function rmStaticsDir($dir_name){
		include_once ("fileoperate.class.php");
		$obj_file = new FileOperate();
		$obj_file->fullRmdir($dir_name);
		return true;
	}
	
}
?>